<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设备巡检日志记录</title>
    #parse("sys/header.html")
    <link href='${rc.contextPath}/statics/plugins/fullcalendar/fullcalendar.css' rel='stylesheet' />
    <link href='${rc.contextPath}/statics/plugins/fullcalendar/fullcalendar.print.css' rel='stylesheet' media='print' />
    <script src='${rc.contextPath}/statics/plugins/fullcalendar/lib/moment.min.js'></script>
    <script src='${rc.contextPath}/statics/plugins/fullcalendar/lib/jquery.min.js'></script>
    <script src='${rc.contextPath}/statics/plugins/fullcalendar/fullcalendar.js'></script>
    <script src='${rc.contextPath}/statics/plugins/fullcalendar/locale/zh-cn.js'></script>
</head>
<style>

    body {
        margin: 40px 10px;
        padding: 0;
        font-family: Arial, Helvetica Neue, Helvetica, sans-serif;
        font-size: 14px;
    }

    #calendar {
        max-width: 900px;
        margin: 0 auto;
    }

</style>
<body>
    <div id='calendar'></div>
</body>

</html>
<script>
    var devId = getQueryString("devId");

    var calendar;
    $(document).ready(function() {
        calendar= $('#calendar').fullCalendar({
            defaultDate: getNowDate(),
            selectable: true,
            selectMirror: true,
            displayEventEnd: true,
            //不显示上月和下月信息
            showNonCurrentDates:false,
            buttonIcons: true, // show the prev/next text
            navLinks: false,
            editable: true,
            eventLimit: true,
            //进入日历界面进行加载添加过的数
            dayClick: function(date, jsEvent, view) {
                var nowDate = date.format();
                //执行弹框显示记录
                openWindow({
                    type: 2,
                    title: '<i class="fa fa-file-text-o"></i> 查看'+nowDate+'详细记录',
                    content: '../device/inspectionrecord_dev.html?devId='+devId+'&nowDate='+nowDate
                });

            },
            events: function(start,end,timezone, callback) {

                var dataResult = getResultByDate(start);
                var month = dataResult.month;
                var year = dataResult.year;
                var day = dataResult.day;
                var num = dataResult.num;

                var events = [];
                Ajax.request({
                    url: '../inspectionrecordingjoin/loadDay',
                    type: 'POST',
                    params: {"devId": devId,'month':month,'year':year},
                    async: false,
                    successCallback: function (r) {
                        var data = r.result;
                        var days = data.split(",");
                        var tempDate = year + "-" + month + "-";

                        //进行日期判断，如果是本月，那本月数量获取到当前日期
                        var nowDateStr = getNowDate();
                        var nowDate = getResultByDate(nowDateStr);

                        //当日数据
                        var tempDay = nowDate.day;
                        var tempMonth = nowDate.month;
                        var tempYear = nowDate.year;

                        //定义变量
                        var dayNum = 0;


                        if(year>2018&year<tempYear){
                            dayNum = num;
                        }else if(year==tempYear){
                            if(month<tempMonth){
                                dayNum = num;
                            }else if(month==tempMonth){
                                dayNum = tempDay;
                            }
                        }

                        //根据月获取日数量 准备events数据
                        for(var i=0;i<dayNum;i++){
                            var md = i+1;
                            //巡检日期
                            var no =tempDate+md+" 23:00:00";
                            var title = "未巡检";
                            var color = "red";
                            //遍历获取的数组
                            for(j = 0,len=days.length; j < len; j++) {
                                var day = days[j];
                                if(md==day){
                                    title="已巡检";
                                    color="blue";
                                    break;
                                }
                            }
                            var event ={
                                id:md,
                                title:title,
                                start:new Date(no).toISOString(),
                                color:color
                            }
                            events.push(event);
                        }
                    }

                });
                callback(events);
            }

        });

    });

    //时间函数
    function timeChange(s) {
        var now = new Date(s);
        var month = now.getMonth()+2;
        if(month<10){
            month = "0"+month;
        }
        var date = {
            start: s,
            end: now.getFullYear() + "-" + month + "-01"
        }
        var data = {
            startDate: date.start,
            endDate: date.end,
            pageNumber: 1,
            pageSize: 1000,
        }
        return data;
    }

    /**
     * 获取当前时间
     * @returns {string}
     */
    function getNowDate(){

        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        if (month < 10) {
            month = "0" + month;
        }
        if (day < 10) {
            day = "0" + day;
        }
        var nowDate = year + "-" + month + "-" + day;
        return nowDate;
    }

    /**
     * 根据日期获取 日 月 年
     * @param date
     * @returns {*}
     */
    function getResultByDate(date) {
        var d = new Date(date);
        var nowMonth = d.getMonth()+1;
        var year = d.getFullYear();
        var day = d.getDate();

        d.setMonth(nowMonth);
        d.setDate(0);
        var num = d.getDate();

        var result={
            month:nowMonth,
            year:year,
            day:day,
            num:num
        };

        return result;
    }


</script>
